﻿namespace Trader.Indicators.TechnicalAnalysis
{
    using System.Collections.Generic;
    using Interface;

    public class SignalsFactory : ISignalsFactory
    {
        private List<ISignal> allIndicators;

        #region Private Member's

        ///<summary>
        ///The maximum day period for any indicator</summary>
        private const int maxPeriodLength = 42;

        ///<summary>
        ///The minimum day period for any indicator</summary>
        private const int minPeriodLength = 14;

        #endregion

        #region ISignalsFactory Members

        ///<summary>
        ///Create all indicators families
        ///</summary>
        ///<returns>List of indicators</returns>
        public IList<ISignal> Signals
        {
            get
            {
                if (allIndicators == null)
                {
                    allIndicators = new List<ISignal>();
                    allIndicators.AddRange(CreateRectanglesFamily());
                    allIndicators.AddRange(CreateTrianglesFamily());
                    allIndicators.AddRange(CreateFlagsFamily());
                    allIndicators.AddRange(CreateHeadAndShouldersFamily());
                    allIndicators.AddRange(CreateWebHitsFamily());
                    allIndicators.AddRange(CreateWebHitsInverseFamily());
                }

                return allIndicators.AsReadOnly();
            }
        }

        #endregion

        ///<summary>
        ///Create Rectangles Family
        ///</summary>
        ///<returns></returns>
        private static IList<ISignal> CreateRectanglesFamily()
        {
            var indicators = new List<ISignal>();
            //Create indicators with seven days difference
            for (int i = minPeriodLength; i <= maxPeriodLength; i += 14)
            {
                indicators.Add(new RectangleSignal(i));
            }
            return indicators;
        }

        ///<summary>
        ///Create Triangles Family
        ///</summary>
        ///<returns></returns>
        private static IList<ISignal> CreateTrianglesFamily()
        {
            var indicators = new List<ISignal>();
            //Create indicators with seven days difference
            for (int i = minPeriodLength; i <= maxPeriodLength; i += 14)
            {
                indicators.Add(new TriangleSignal(i));
            }
            return indicators;
        }

        ///<summary>
        ///Create Flags Family
        ///</summary>
        ///<returns></returns>
        private static IList<ISignal> CreateFlagsFamily()
        {
            var indicators = new List<ISignal>();
            //Create indicators with seven days difference
            for (int i = minPeriodLength; i <= maxPeriodLength; i += 14)
            {
                indicators.Add(new FlagSignal(i));
            }
            return indicators;
        }

        ///<summary>
        ///Create head and Shoulders Family
        ///</summary>
        ///<returns></returns>
        private static IList<ISignal> CreateHeadAndShouldersFamily()
        {
            var indicators = new List<ISignal>();
            //Create indicators with seven days difference
            for (int i = minPeriodLength; i <= maxPeriodLength; i += 14)
            {
                indicators.Add(new HeadAndShouldersSignal(i));
            }
            return indicators;
        }

        ///<summary>
        ///Create Web Hits Family
        ///</summary>
        ///<returns></returns>
        private static IList<ISignal> CreateWebHitsFamily()
        {
            var indicators = new List<ISignal>();
            //Create indicators with seven days difference
            for (int i = minPeriodLength; i <= maxPeriodLength; i += 14)
            {
                indicators.Add(new WebHitsSignal(i));
            }
            return indicators;
        }

        ///<summary>
        ///Create Web Hits Family
        ///</summary>
        ///<returns></returns>
        private static IList<ISignal> CreateWebHitsInverseFamily()
        {
            var indicators = new List<ISignal>();
            //Create indicators with seven days difference
            for (int i = minPeriodLength; i <= maxPeriodLength; i += 14)
            {
                indicators.Add(new WebHitsInverseSignal(i));
            }
            return indicators;
        }
    }
}